<template>
  <page-header-wrapper :title="false">
    <a-card title="订单详情">
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="订单" :span="3"> {{ info?.id }} </a-descriptions-item>
        <a-descriptions-item label="创建人" :span="3"> {{ info?.operatorName }} </a-descriptions-item>
        <a-descriptions-item label="日期" :span="3"> {{ info?.operTime }} </a-descriptions-item>
        <a-descriptions-item label="金额" :span="3"> {{ info?.chgValue }} </a-descriptions-item>
        <a-descriptions-item label="账户" :span="3"> {{ info?.accName }} </a-descriptions-item>
        <!-- <a-descriptions-item label="状态" :span="3">
          {{ info?.chgType === '0' ? '未付款' : info?.orderStatus === '1' ? '已付款' : '撤销' }}
        </a-descriptions-item> -->
        <a-descriptions-item label="审核状态" :span="3">
          {{ info?.checkerStatus === '1' ? '已审核' : '未审核' }}
        </a-descriptions-item>
        <a-descriptions-item label="审核人" :span="3" v-if="info?.checkStatus === '1'">
          {{ info?.checkerName }}
        </a-descriptions-item>
        <a-descriptions-item label="备注" :span="3"> {{ info?.memo }} </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex">
            <a-button
              type="primary"
              style="margin: 0px 5px"
              @click="handleEdit(info)"
              v-if="info?.checkerStatus === '0'"
            >编辑订单</a-button
            >
            <a-button type="danger" style="margin-right: 5px" @click="handleBack()" v-if="info?.checkerStatus === '0'">撤销订单</a-button
            >
            <a-button
              type="primary"
              style="margin: 0px 5px"
              v-if="info?.checkerStatus === '0'"
              @click="handleCheckPur()"
            >审核订单</a-button
            >
            <a-button
              type="success"
              v-if="info?.checkerStatus === '1'"
              style="margin: 0px 5px"
              @click="handleUnCheckPur()"
            >反审核</a-button
            >
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="操作记录" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="8">
              <a-form-item label="日期">
                <a-input v-model="queryParam.keyword" placeholder="请输入"/>
              </a-form-item>
            </a-col> -->
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="recordLoading" @click="handleSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- <div class="table-operator">
        <a-button icon="cloud-upload">导出</a-button>
      </div> -->

      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <!-- <span slot="status" slot-scope="text, record">
          <template>
            <a-tag color="#82af6f" v-if="record?.status == 1">已审核</a-tag>
            <a-tag color="#abbac3" v-else>未审核</a-tag>
          </template>
        </span> -->
        <span slot="action" slot-scope="text, record">
          <template> {{ record.empName }} <a-tag color="#428bca" @click="handleUserDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
      <record-form
        ref="recordModal"
        :visible="recordVisible"
        :loading="recordConfirmLoading"
        :model="recordMdl"
        @cancel="handleRecordCancel"
        @ok="handleRecordOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getOperRecordListUsingPOST } from '@/api/api/caigouguanli.ts'
import RecordForm from './RecordForm'
import {
  getStockRecordUsingGET,
  revokeStockRecordUsingPOST,
  stockRecordCheckPurUsingPOST,
  stockRecordUnCheckPurUsingPOST,
  updateStockRecordUsingPOST
} from '@/api/api/qiantaizhangwu.ts'
export default {
  name: 'CashierShareholderRecordDetail',
  components: {
    RecordForm,
  },
  data() {
    return {
      mdl: null,
      visible: false,
      confirmLoading: false,
      recordMdl: null,
      recordVisible: false,
      recordConfirmLoading: false,
      turnMdl: null,
      turnVisible: false,
      turnConfirmLoading: false,
      recordLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      info: {},
      advanced: false,
      columns: [
        {
          title: '日期',
          dataIndex: 'operTime',
        },
        {
          title: '类型',
          dataIndex: 'action',
        },
        {
          title: '操作者',
          dataIndex: 'option',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
    }
  },
  mounted() {
    this.getDetail()
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleUserDetail(record) {
      this.$router.push({ path: `/user/user/${record.operator}` })
    },
    handleEdit(info) {
      this.recordMdl = {...info}
      this.recordVisible = true
    },
    handleRecordOk() {
      this.recordConfirmLoading = true
      const form1 = this.$refs.recordModal.form
      form1.validateFields((errors, values) => {
        if (!errors) {
          updateStockRecordUsingPOST({ ...values,operTime:values.operTime})
            .then(() => {
              this.handleSearch(this.queryParam)
              this.getDetail()
              this.recordVisible = false
              this.recordConfirmLoading = false
              this.$message.success('修改成功')
            })
            .catch(() => {
              this.recordConfirmLoading = false
              this.$message.error('修改失败,请联系管理员')
            })
        } else {
          this.recordConfirmLoading = false
        }
      })
    },
    handleRecordCancel() {
      this.recordVisible = false
      const form = this.$refs.recordModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleCheckPur() {
      const that = this
      that.$confirm({
        title: '审核通过',
        content: '你确定要审核通过该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          stockRecordCheckPurUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.handleSearch(that.queryParam)
              that.handleRecordSearch(that.queryParam)
              that.getDetail()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleUnCheckPur() {
      const that = this
      that.$confirm({
        title: '反审核',
        content: '你确定要反审核该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          stockRecordUnCheckPurUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.handleRecordSearch(that.queryParam)
              that.handleSearch(that.queryParam)
              that.getDetail()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleBack() {
      const that = this
      that.$confirm({
        title: '撤销订单',
        content: '你确定要撤销该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          revokeStockRecordUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.$router.back()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    getDetail() {
      getStockRecordUsingGET({ id: this.$route.params.id })
        .then((res) => {
          this.info = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(params) {
      this.recordLoading = true
      getOperRecordListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.loadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>
